home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / asm / a86v401.zip / A06A.DOC < prev    next >
Text File  |  1995-03-05  |  21KB  |  420 lines

  1. 3h CHAPTER 6    THE 86 INSTRUCTION SET
  2.  
  3. In this chapter we discuss in detail the instruction set
  4. supported by both the A86 and A386 assemblers.  To use any of the
  5. 32-bit registers, the extra segment regsiters FS and GS, or the
  6. instructions marked with a "3", "4", or "5" in the instruction
  7. list, you need my A386 assembler, available only if you register
  8. both A86 and D86.
  9.  
  10.  
  11. Effective Addresses
  12.  
  13. Most memory data accessing in the 86 family is accomplished via
  14. the mechanism of the effective address.  Wherever an effective
  15. address specifier "eb", "ew" "ed", or "ev" appears in the list of
  16. instructions, you may use a wide variety of actual operands in
  17. that instruction.  These include general registers, memory
  18. variables, and a variety of indexed memory quantities.
  19.  
  20. GENERAL REGISTERS: Wherever an "ew" appears, you can use any of
  21. the 16-bit registers AX,BX,CX,DX,SI,DI,SP, or BP.  Wherever an
  22. "eb" appears, you can use any of the 8-bit registers
  23. AL,BL,CL,DL,AH,BH,CH, or DH.  Whenever an "ed" occurs, you can
  24. (on A386) use any of the 32-bit registers
  25. EAX,EBX,ECX,EDX,ESI,EDI,EBP, or ESP.  For A86, "ev" is the same
  26. as "ew"; for A386, it means you can use either a 16-bit or a
  27. 32-bit register.  For example, the "ADD ev,rv" form subsumes the
  28. 16-bit register-to-register adds; for example, ADD AX,BX; ADD
  29. SI,BP; ADD SP,AX.  On A386, this form also includes 32-bit
  30. register-to-register adds; e.g., ADD EBX,ESI.  At the machine
  31. level the 16-vs.-32 distinction is made by an "operand override"
  32. opcode byte, 66H, that A386 places before the instruction to
  33. signal a switch from 16-bits to 32-bits.
  34.  
  35. MEMORY VARIABLES: Wherever an "eb", "ew", "ed", or "ev" appears,
  36. you can use a memory variable of the indicated size: byte, word,
  37. doubleword, or either-word-or-doubleword.  Variables are
  38. typically declared in the DATA segment, using a DD declaration
  39. for a doubleword variable, a DW declaration for a word variable,
  40. or a DB declaration for a byte variable.  For example, you can
  41. declare variables:
  42.  
  43.      DATA_PTR  DW ?
  44.      ESC_CHAR  DB ?
  45.  
  46. Later, you can load or store these variables:
  47.  
  48.      MOV ESC_CHAR,BL    ; store the byte variable ESC_CHAR
  49.      MOV DATA_PTR,081   ; initialize DATA_PTR
  50.      MOV SI,DATA_PTR    ; load DATA_PTR into SI for use
  51.      LODSW              ; fetch the word pointed to by DATA_PTR
  52.  
  53. Alternatively, you can address specific unnamed memory locations
  54. by enclosing the location value in square brackets; for example,
  55.  
  56.      MOV AL,[02000]     ; load contents of location 02000 into AL
  57.                                                               6-2
  58.  
  59. Note that A86 discerned from context (loading into AL) that a
  60. BYTE at 02000 was intended.  Sometimes this is impossible, and
  61. you must specify byte or word:
  62.  
  63.      INC B[02000]       ; increment the byte at location 02000
  64.      MOV W[02000],0     ; set the WORD at location 02000 to zero
  65.  
  66.  
  67. INDEXED MEMORY: The 86 supports the use of certain registers as
  68. base pointers and index registers into memory.  BX and BP are the
  69. base registers; SI and DI are the index registers.  You may
  70. combine at most one base register, at most one index register,
  71. and a constant number into a run time pointer that determines the
  72. location of the effective address memory to be used in the
  73. instruction.  These can be given explicitly, by enclosing the
  74. index registers in brackets:
  75.  
  76.      MOV AX,[BX]
  77.      MOV CX,W[SI+17]
  78.      MOV AX,[BX+SI+5]
  79.      MOV AX,[BX][SI]5   ; another way to write the same instr.
  80.  
  81. Or, indexing can be accomplished by declaring variables in a
  82. based structure (see the STRUC directive in Chapter 9):
  83.  
  84.      STRUC [BP]        ; NOTE: based structures are unique to A86!
  85.        BP_SAVE   DW ?  ; BP_SAVE is a word at [BP]
  86.        RET_ADDR  DW ?  ; RET_ADDR is a word at [BP+2]
  87.        PARM1     DW ?  ; PARM1 is a word at [BP+4]
  88.        PARM2     DW ?  ; PARM2 is a word at [BP+6]
  89.      ENDS              ; end of structure
  90.      INC PARM1         ; equivalent to INC W[BP+4]
  91.  
  92. Finally, indexing can be done by mixing explicit components with
  93. declared ones:
  94.  
  95.      TABLE DB 4,2,1,3,5
  96.      MOV AL,TABLE[BX]      ; load byte number BX of TABLE
  97.  
  98. The 386 processor also supports indexing using any of the eight
  99. 32-bit general registers.  This type of indexing is of limited
  100. use for memory referencing from real-mode programs (most programs
  101. running under DOS), since offsets greater than 64K are disallowed
  102. in real mode (you will get a General Protection Fault if you try
  103. it).  32-bit indexing is, however, useful in conjunction with the
  104. LEA instruction, giving an extremely powerful register arithmetic
  105. instruction.  For example, LEA ECX,[EAX+2*EBX+17000] performs two
  106. additions and a multiplication, all in a single machine
  107. instruction.  Since no memory accessed is actually attempted,
  108. this kind of LEA usage is allowed in real-mode DOS programs.
  109.                                                               6-3
  110.  
  111. In 32-bit indexing, you may use one or two of any of the 32-bit
  112. general registers.  You may also scale one of the indexing
  113. registers, by multiplying it by 2, 4, or 8.  You may also add or
  114. subtract a constant of any size up to a doubleword capacity to
  115. the indexed quantity.  If you use the same register twice and
  116. scale one of the instances of that register, you get, in effect,
  117. an odd-number scaling (3, 5, or 9) of that register; e.g., A386
  118. will allow LEA EAX,[9*EBX] as an abbreviation for LEA
  119. EAX,[8*EBX+EBX].
  120.  
  121. Due to coding restrictions, the ESP register can be used only
  122. once within an indexed quantity, and cannot be scaled.
  123.  
  124. Some more examples of 32-bit indexing are:
  125.  
  126.      XCHG DX,[EAX]
  127.      MOV AL,[EAX+EBX]
  128.      ADD EBX,[ESI+8*ECX+3391811]
  129.      LEA ECX,[4*EBX-7]
  130.  
  131.  
  132.  
  133. Segmentation and Effective Addresses
  134.  
  135. The 86 family has four segment registers, CS, DS, ES, and SS,
  136. used to address memory.  The 386 and later processors add two
  137. more segment registers FS and GS.  Each segment register points
  138. to 64K bytes of memory within the 1-megabyte memory space of the
  139. 86. (The start of the 64K is calculated by multiplying the
  140. segment register value by 16; i.e., by shifting the value left by
  141. one hex digit.)  If your program's code, data and stack areas can
  142. all fit in the same 64K bytes, you can leave all the segment
  143. registers set to the same value.  In that case, you won't have to
  144. think about segment registers: no matter which one is used to
  145. address memory, you'll still get the same 64K.  If your program
  146. needs more than 64K, you must point one or more segment registers
  147. to other parts of the memory space.  In this case, you must take
  148. care that your memory references use the segment registers you
  149. intended.
  150.  
  151. Each effective address memory access has a default segment
  152. register, to be used if you do not explicitly specify which
  153. segment register you wish.  For most effective addresses, the
  154. default segment register is DS.  The exceptions are those
  155. effective addresses that use the BP register for indexing.  All
  156. BP-indexed memory references have a default of SS.  (This is
  157. because BP is intended to be used for addressing local variables,
  158. stored on the stack.)
  159.                                                               6-4
  160.  
  161. If you wish your memory access to use a different segment
  162. register, you provide a segment override byte before the
  163. instruction containing the effective address operand.  In the A86
  164. language, you code the override by giving the name of the segment
  165. register you wish before the instruction mnemonic.  For example,
  166. suppose you want to load the AL register with the memory byte
  167. pointed to by BX.  If you code MOV AL,[BX], the DS register will
  168. be used to determine which 64K segment BX is pointing to.  If you
  169. want the byte to come from the CS-segment instead, you code CS
  170. MOV AL,[BX].  Be aware that the segment override byte has effect
  171. only upon the single instruction that follows it.  If you have a
  172. sequence of instructions requiring overrides, you must give an
  173. override byte before every instruction in the sequence.  (In that
  174. case, you may wish to consider changing the value of the default
  175. segment register for the duration of the sequence.)
  176.  
  177. NOTE: This method for providing segment overrides is unique to
  178. the A86 assembler!  The assemblers provided by Intel and IBM
  179. (MS-DOS) attempt to figure out segment allocation for you, and
  180. plug in segment override bytes "behind your back".  In order to
  181. do this, those assemblers require you to inform them which
  182. variables and structures are pointed to by which segment
  183. registers.  That is what the ASSUME directive in those assemblers
  184. is all about.  I wrote Intel's first 86 assembler, ASM86, so I
  185. have been watching the situation since day one.  Over the years,
  186. I have concluded that the ASSUME mechanism creates far, far more
  187. confusion that it solves.  So I scrapped it; and the result is an
  188. assembler with far less red tape.  But if your program needs more
  189. than 64K, you do have to manage those segment registers yourself;
  190. so take care!
  191.  
  192.  
  193. Effective Use of Effective Addresses
  194.  
  195. Remember that all of the common instructions of the 86 family
  196. allow effective addresses as operands.  (The only major functions
  197. that don't are the AL/AX specific ones: multiply, divide, and
  198. input/output).  This means that you don't have to funnel many
  199. numbers through AL or AX just to do something with them.  You can
  200. perform all the common arithmetic, PUSH/POP, and MOVes from any
  201. general register to any general register; from any memory
  202. location (indexed if you like) to any register; and (this is most
  203. often overlooked) from any register TO memory.  The only thing
  204. you can't do in general is memory-to-memory.  Among the more
  205. common operations that inexperienced 86 programmers overlook are:
  206.  
  207.    * setting memory variables to immediate values
  208.  
  209.    * testing memory variables, and comparing them to constants
  210.  
  211.    * preserving memory variables by PUSHing and POPping them
  212.  
  213.    * incrementing and decrementing memory variables
  214.  
  215.    * adding into memory variables
  216.                                                               6-5
  217.  
  218. Encoding of Effective Addresses
  219.  
  220. This section outlines the number of program opcode bytes
  221. generated by effective-address specifications.  This will let you
  222. make judgments when trying to keep your program as small as
  223. possible.  The precise opcodes generated are explained in the
  224. text files EFF86.DOC in the A86 package, and EFF386.DOC in the
  225. A386 package.
  226.  
  227. Every instruction with an 16-bit effective address has an encoded
  228. byte, known as the effective address byte, following the
  229. instruction's main opcode.  (For obscure reasons, Intel calls
  230. this byte the ModRM byte.)  If the effective address is a memory
  231. variable, or an indexed memory location with a non-zero constant
  232. offset, then the effective address byte is immediately followed
  233. by the offset amount.  Amounts in the range -128 to +127 are
  234. given by a single signed byte.  Amounts outside that range are
  235. represented by a 2-byte offset.
  236.  
  237. In the instruction chart given later in this chapter,
  238. effective-address specification opcodes are denoted by a slash /
  239. followed either by the letter "r" or an octal digit.  The meaning
  240. of the r-or-digit is explained in the EFF*.DOC files.  For
  241. example, the instruction DIV CX falls under the DIV eb form in
  242. the instruction chart.  The instruction occupies two bytes: the
  243. main opcode byte 0F6H, followed by a single effective address
  244. byte with no constant offsets involved.  Similarly, the
  245. instruction DIV B[BX] occupies two bytes.  For DIV B[BX+7] you
  246. must add an offset byte for the 7, making a total of three bytes.
  247. For DIV B[BX+1000] you must add a 2-byte offset for the 1000,
  248. making a total of 4 bytes.  For DIV B[02000] (more typically
  249. coded with a symbolic name such as DIV MY_VAR_NAME), the
  250. instruction is also 4 bytes: the main opcode byte, the effective
  251. address byte, and the offset of the memory variable.
  252.  
  253. An anomalous case is the operand [BP].  The effective-address
  254. byte encoding for this particular operand was usurped by the
  255. simple-variable case.  When A86 sees [BP], it must specify an
  256. 8-bit offset whose value is zero.  Thus, the instruction DIV
  257. B[BP] occupies three bytes, not two. This anomaly does not apply
  258. to [BP+SI] or [BP+DI].
  259.  
  260. In A386, 32-bit indexing is signalled by a special address
  261. override opcode byte (67H) preceding the instruction.  Following
  262. the override byte is the instruction's main opcode, followed by
  263. the effective-address specification.  For a simple memory
  264. variable, the specification consists of a single
  265. effective-address byte followed by the 4-byte offset of the
  266. variable.  For indexing involving a single, non-scaled index
  267. register other than ESP, the specification consists of a single
  268. byte followed by the constant offset component.  For indexing
  269. involving two registers, scaling, or the ESP register, there are
  270. two bytes followed by the constant offset component.  The
  271. constant offset component occpies no space if the the offset is
  272. zero, one byte if between -128 to +127, and 4 bytes otherwise.
  273. There is no provision for a 16-bit-word-sized offset if you are
  274. using 32-bit indexing.
  275.                                                               6-6
  276.  
  277. Note the distinction between the address override byte (67H) and
  278. the operand override byte (66H).  A86 must supply an address
  279. override when the instruction involves a memory operand whose
  280. address has 32 bits.  A86 must supply an operand override when
  281. the data being manipulated has 32 bits.  In general, when a
  282. 32-bit register name appears inside the square brackets, that's
  283. an address override; when it appears outside the square brackets,
  284. that's an operand override.  Examples:
  285.  
  286.      MOV DX,[BX]      ; needs neither override in a 16-bit segment
  287.      MOV DX,[EBX]     ; needs an address override
  288.      MOV EDX,[BX]     ; needs an operand override
  289.      MOV EDX,[EBX]    ; needs both overrides
  290.  
  291. Also note that the generation of these override bytes is handled
  292. automatically by A86 when it scans the operands to an
  293. instruction.  The only exceptions to this are the no-operand
  294. string operations: REP MOVSW, LODSD, SCASB, etc.  For these
  295. instructions, the operand size is signalled by the last letter
  296. (B, W, or D) of the mnemonic; however, the addressing mode is not
  297. signalled by the mnemonic.  If you are in 16-bit mode, as all
  298. simple DOS programs are, you need to precede a string instruction
  299. with an explicit A4 prefix if you wish to use 32-bit addressing
  300. ([ESI] and/or [EDI] with count ECX).  If you are assembling to a
  301. 32-bit protected-mode segment (when that is implemented) you will
  302. need to use an explicit A2 prefix if you wish to use 16-bit
  303. addressing ([SI] and/or [DI] with count CX).
  304.  
  305. Here are some examples of instruction size involving 32-bit
  306. indexing in a real-mode segment: DIV B[EBX] requires an address
  307. override byte, the single instruction opcode byte 0F6H, and an
  308. effective address byte: total 3 bytes.  DIV B[EBX+7] adds the
  309. offset byte 07, making the total 4 bytes.  DIV B[EBX+1000] forces
  310. the offset to be 4 bytes, making the total 7 bytes.  DIV
  311. B[EBX+EDI*2] does not require an offset, but the extra index
  312. register expands the effective address specifier to two bytes,
  313. making the total 4 bytes.  Similarly, DIV B[ESP] requires two
  314. effective address bytes (total 4 instruction bytes), because the
  315. ESP register is a special case.  Finally, DIV
  316. ES:D[EBX+EDI*2+1000] requires three overrides (segment override
  317. ES, operand override for the D, and address override for 32-bit
  318. indexing), the main opcode byte, two effective address opcode
  319. bytes, and a 4-byte offset: total 10 bytes.
  320.  
  321. The [BP] extra-byte anomaly applies, in 32-bit mode, to [EBP] as
  322. well.  In fact, the anomaly also applies when another indexing
  323. register (scaled or not) is added to [EBP].  A386 must generate
  324. an offset byte whose value is 0 when it sees any no-offset forms
  325. involving [EBP].
  326.                                                               6-7
  327.  
  328. The 386 and later processors, when running in protected mode,
  329. allow segments whose default word-size is 32 bits instead of
  330. 16-bits.  In such segments, the usage of the operand and address
  331. override bytes is reversed: 32-bit operands do not require the
  332. operand-override byte, and 16-bit operands do.  (8-bit operands
  333. never require an operand-override byte.) 32-bit memory addresses
  334. do not require an address-override byte; 16-bit addresses do.
  335. This mode will be recognized by A386 whenever the USE32 directive
  336. is used; however, at the time of this writing, this feature is
  337. not yet implemented.  All DOS programs, which run in real mode,
  338. have a default of 16 bits.
  339.  
  340.  
  341. How to Read the Instruction Set Chart
  342.  
  343. The following chart summarizes the machine instructions you can
  344. program with A86.  In order to use the chart, you need to learn
  345. the meanings of the specifiers (each given by 2 lower case
  346. letters) that follow most of the instruction mnemonics.  Each
  347. specifier indicates the type of operand (register byte, immediate
  348. word, etc.) that follows the mnemonic to produce the given
  349. opcodes.  The "v" type, for A86, is the same as "w" -- it denotes
  350. a 16-bit word.  On A386, "v" denotes either a word or doubleword,
  351. depending on the presence of an operand override prefix byte.
  352.  
  353. "c"  means the operand is a code label, pointing to a part of the
  354.     program to be jumped to or called.  A86 will also accept a
  355.     constant offset in this place (or a constant segment-offset
  356.     pair in the case of "cp").  "cb" is a label within about 128
  357.     bytes (in either direction) of the current location.  "cv" is
  358.     a label within the same code segment as this program; "cp" is
  359.     a pair of constants separated by a colon-- the segment value
  360.     to the left of the colon, and the offset to the right.  The
  361.     offset is always a word in A86; it can be either a word or a
  362.     doubleword in A386.  Note that in both the cb and cv cases,
  363.     the object code generated is the offset from the location
  364.     following the current instruction, not the absolute location
  365.     of the label operand.  In some assemblers (most notably for
  366.     the Z-80 processor) you have to code this offset explicitly
  367.     by putting "$-" before every relative jump operand in your
  368.     source code.  You do NOT need to, and should not do so with
  369.     A86.
  370.  
  371. "e"  means the operand is an Effective Address.  The concept of
  372.     an Effective Address is central to the 86 machine
  373.     architecture, and thus to 86 assembly language programming.
  374.     It is described in detail at the start of this chapter.  We
  375.     summarize here by saying that an Effective Address is either
  376.     a general purpose register, a memory variable, or an indexed
  377.     memory quantity.  For example, the instruction "ADD rb,eb"
  378.     includes the instructions: ADD AL,BL, and ADD CH,BYTEVAR, and
  379.     ADD DL,B[BX+17].
  380.                                                               6-8
  381.  
  382. "i"  means the operand is an immediate constant, provided as part
  383.     of the instruction itself.  "ib" is a byte-sized constant;
  384.     "iw" is a constant occupying a full 16-bit word.  The operand
  385.     can also be a label, defined with a colon.  In that case, the
  386.     immediate constant which is the location of the label is
  387.     used.  Examples:  "MOV rw,iw" includes the instructions: MOV
  388.     AX,17, or MOV SI,VAR_ARRAY, where "VAR_ARRAY:" appears
  389.     somewhere in the program, defined with a colon.  NOTE that if
  390.     VAR_ARRAY were defined without a colon, e.g., "VAR_ARRAY DW
  391.     1,2,3", then "MOV SI,VAR_ARRAY" would be a "MOV rw,ew" NOT a
  392.     "MOV rw,iw".  The MOV would move the contents of memory at
  393.     VAR_ARRAY (in this case 1) into SI, instead of the location
  394.     of the memory. To load the location, you can code "MOV
  395.     SI,OFFSET VAR_ARRAY".
  396.  
  397. "m"  means a memory variable or an indexed memory quantity; i.e.,
  398.     any Effective Address EXCEPT a register.
  399.  
  400. "r"  means the operand is a general purpose register.  The 8 "rb"
  401.     registers are AL,BL,CL,DL,AH,BH,CH,DH; the 8 "rw" registers
  402.     are AX,BX,CX,DX,SI,DI,BP,SP.
  403.  
  404.     "rv/m" is used in the Bit Test instructions to denote either
  405.     a word-or-doubleword register, or an array of bits in memory
  406.     that can any length.
  407.  
  408. NOTE: The following chart gives all instructions for all
  409. processors through the Pentium.  You must take care to use only
  410. the instructions appropriate for the target processor of your
  411. program (the P switch will enforce this for you: see Chapter 3).
  412. If an instruction form does not run on all processors, there is a
  413. letter or digit just before the description field.  "N" means the
  414. instruction runs only on NEC processors (which are rare nowdays).
  415. A digit x means the instruction runs on the x86 or later: 1 for
  416. 186, 2 for 286, 3 for 386, 4 for 486, 5 for Pentium.
  417. Instructions with 3 or greater are recognized only by my A386
  418. assembler, received only by those who register both A86 and D86.
  419.  
  420.